package sw

import (
	"crypto/rand"

	"github.com/hyperledger/fabric/bccsp"
)

type sm2Signer struct{}

func (s *sm2Signer) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) ([]byte, error) {
	return k.(*sm2PrivateKey).privKey.Sign(rand.Reader, digest, nil)
}

type sm2PrivateKeyVerifier struct{}

func (v *sm2PrivateKeyVerifier) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (bool, error) {
	return k.(*sm2PrivateKey).privKey.Verify(digest, signature), nil
}

type sm2PublicKeyVerifier struct{}

func (v *sm2PublicKeyVerifier) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (bool, error) {
	return k.(*sm2PublicKey).pubkey.Verify(digest, signature), nil
}

type sm2Encryptor struct{}

func (e *sm2Encryptor) Encrypt(k bccsp.Key, src []byte, opts bccsp.EncrypterOpts) ([]byte, error) {
	return k.(*sm2PrivateKey).privKey.PublicKey.Encrypt(src)
}

type sm2Decryptor struct{}

func (*sm2Decryptor) Decrypt(k bccsp.Key, src []byte, opts bccsp.DecrypterOpts) ([]byte, error) {
	return k.(*sm2PrivateKey).privKey.Decrypt(src)
}
